package com.icebergteam.warfacewidget.rank.ui;

import java.util.ArrayList;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.icebergteam.warfacewidget.R;
import com.icebergteam.warfacewidget.rank.ClassHelper;
import com.icebergteam.warfacewidget.rank.RankHelper;
import com.icebergteam.warfacewidget.rank.data.Top100Item;

public class Top100ModelAdapter extends ArrayAdapter<Top100Item> {
	public final String className = Top100ModelAdapter.class.getCanonicalName();
	
	private ArrayList<Top100Item> listTop100Items;
	
	public Top100ModelAdapter(Context context, int textViewResourceId, ArrayList<Top100Item> top100) {
		super(context, textViewResourceId, top100);
		
		this.listTop100Items = top100;
	}
	
	static public View getViewWithParam(Context context, View convertView, Top100Item top100item) {
		View v = convertView;
		if(v == null) {
			LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.top100_list_item_widget_layout, null);
		}
		
		if(top100item != null) {
			int rank = top100item.getPos();
			
			
			ImageView rankImageView = (ImageView) v.findViewById(R.id.widget_rank_top100_list_item_rank_image);
			TextView posTextView = (TextView) v.findViewById(R.id.widget_rank_top100_list_item_pos);
			TextView nicknameTextView = (TextView) v.findViewById(R.id.widget_rank_top100_list_item_nickname);
			ImageView classImageView = (ImageView) v.findViewById(R.id.widget_rank_top100_list_item_class);
			ProgressBar progressBar = (ProgressBar) v.findViewById(R.id.widget_rank_top100_list_item_pos_progress);
			
			
			rankImageView.setImageResource(RankHelper.getImageIdForRank(rank));
			
			
			
			if(rank > 3) {
				posTextView.setText(String.format("%s", rank));
				posTextView.setVisibility(View.VISIBLE);
			} else {
				posTextView.setVisibility(View.GONE);
			}
			
			nicknameTextView.setText(Html.fromHtml(top100item.getNickname()));
			classImageView.setImageResource(ClassHelper.getImageIdForClass(top100item.getClasstime()));
		
			progressBar.setMax(100);
			progressBar.setIndeterminate(false);
			progressBar.setProgress(top100item.getProgress());
		}
		return v;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return getViewWithParam(getContext(), convertView, listTop100Items.get(position));
	}
}